package com.泛型;
/*Generic通用的
* 泛型类：
* */

//学生类
class Student{
    private String name;

    /*  在没有构造方法时，会默认给出无参构造方法；当给出含参
    * 构造方法时，系统不会默认给出无参构造，需要自己补写无参构造方法*/
    public Student(){}
    public Student(String name){
        this.name = name;
    }

    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
}
//老师类
class Teacher{
    private int age;

    public Teacher(){}
    public Teacher(int age){
        this.age = age;
    }

    public void setAge(int age){
        this.age = age;
    }
    public int getAge(){
        return age;
    }
}
//泛型类 此处T可随便写为任意标识，常见的如T,E,K,V等形式参数用于表示泛型
class Generic<T>{
    private T t;

    public void setT(T t){
        this.t = t;
    }
    public T getT(){
        return t;
    }
}

public class GenericDemo1 {
    public static void main(String[] args){
        Student s = new Student();
        s.setName("zhangsan");
        System.out.println(s.getName());

        Teacher t = new Teacher();
        t.setAge(25);
        System.out.println(t.getAge());
        System.out.println("---------------------");
        /*通过使用泛型，原本需要创建n个类,现在只需要一个泛型类,在使用时更改泛型类的参数即可*/
        Generic<String> g = new Generic<String>();
        g.setT("zhangsan");
        System.out.println(g.getT());

        Generic<Integer> g1 = new Generic<Integer>();
        g1.setT(20);
        System.out.println(g1.getT());

        Generic<Boolean> g2 = new Generic<Boolean>();
        g2.setT(true);
        System.out.println(g2.getT());
    }
}
